<template>
    <my-select
        :placeholder="placeholder"
        v-model="model"
        :data="data"
        :props="{value:'code',label:'name'}"
        2way="dicname"
        :dicname.sync="dicnameModel"
    ></my-select>
</template>

<script>
export default {
    props: {
        placeholder: {
            type: String,
            default: '',
        },
        value: {
            type: [Array, String],
            default: '',
        },
        props: {
            type: Object,
            default: () => ({}),
        },
        object: {
            type: Boolean,
            default: false,
        },
        dicname: {
            type: String,
            default: '',
        },
        isSearch: {
             type: Boolean,
            default: false,
        }
    },
    data() {
        return {
            data: [],
        };
    },
    computed: {
        model: {
            get() {
                if (!this.object) {
                    return this.value;
                }

                if (this.multiple) {
                    return this.value.map(item => item[this.props.value]);
                }
                
                var first = this.value[0];

                if (first) {
                    return first[this.props.value];
                }

                return '';
            },
            set(val) {
                if (!this.object) {
                    this.$emit('input', val);
                    return;
                }

                var res = [];

                this.$nextTick(() => {
                    if (this.multiple) {
                        val.forEach((item, index) => {
                            res.push(this.buildItem(this.dicname[index], item));
                        });
                        this.$emit('input', res);

                        return;
                    }

                    res.push(this.buildItem(this.dicname, val));
                    this.$emit('input', res);
                });
            }
        },
        dicnameModel: {
            get() {
                return this.dicname;
            },
            set(val) {
                this.$emit('update:dicname', val);
            }
        }
    },
    methods: {
        buildItem(label, value) {
            return {
                [this.props.value]: value,
                [this.props.label]: label,
            };
        },
        queryData() {
            // filetype/pglist /home/typelist.json  获取文件列表       .file/platformmg/filetype/pglist
            this.$get(`${this.$store.state.api.fileUrl}/platformmg/filetype/pglist.json`, {
                pagesize: 999,
                pageindex: 1,
            }, data => {
                this.data = data.rows;
                if(this.isSearch){
                    this.data.push({ code: '', name: '全部', rowguid: '' });
                }
                console.log(111,this.data);
                
            });
        },
    },
    created() {
        this.queryData();
    },
    watch: {
        'model':{
            handler(n,o){
                // console.log(n);
            },
            deep:true
        },
    },
};
</script>